package com.guo.xiao.bing.service;

import com.guo.xiao.bing.dao.ProjectMapper;
import com.guo.xiao.bing.dao.ProjectTypeMapper;
import com.guo.xiao.bing.entity.Project;
import com.guo.xiao.bing.entity.ProjectExample;
import com.guo.xiao.bing.entity.ProjectType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectTypeService {

    @Autowired
    ProjectTypeMapper projectTypeMapper;
    @Autowired
    ProjectMapper projectMapper;

    public List<ProjectType> find() {
        return projectTypeMapper.selectByExample(null);
    }

    public ProjectType findById(Integer id) {
        return projectTypeMapper.selectByPrimaryKey(id);
    }

    public Integer save(ProjectType projectType) {
        return projectTypeMapper.insert(projectType);
    }

    public Integer update(ProjectType projectType) {
        return projectTypeMapper.updateByPrimaryKeySelective(projectType);
    }

    public Integer delete(Integer id) {
        ProjectExample example = new ProjectExample();
        example.createCriteria().andProjectTypeIdEqualTo(id);
        List<Project> projects = projectMapper.selectByExample(example);
        for (Project p : projects) {
            p.setTitle("该项目类型已删除");
            p.setContent("该项目类型已删除");
            projectMapper.updateByPrimaryKeyWithBLOBs(p);
        }


        return projectTypeMapper.deleteByPrimaryKey(id);
    }
}
